package com.example.springboot1.limit;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.example.springboot1.entity.FillData;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;

/**
 * @author ckj
 * @description TODO
 * @date 2023-03-10 11:16
 */


public class DownWork implements Callable<ResponseEntity<byte[]>> {
    @Override
    public ResponseEntity<byte[]> call() throws Exception {
        ByteArrayOutputStream out = new ByteArrayOutputStream();

        InputStream resourceAsStream = this.getClass().getClassLoader().getResourceAsStream("templates/dd.xlsx");
        //读取Excel
        ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(resourceAsStream).build();
        WriteSheet writeSheet = EasyExcel.writerSheet().build();

        FillData fillData = new FillData();
        fillData.setName("张三");
        fillData.setNumber(5.2);
        excelWriter.fill(fillData, writeSheet);
        List<FillData> data = new ArrayList<>();
        data.add(fillData);
        data.add(fillData);
        FillConfig fillConfig = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
        excelWriter.fill(data, fillConfig, writeSheet);

        Map<String, Object> map = new HashMap<>();
        map.put("date", "2019年10月9日13:28:28");
        excelWriter.fill(map, writeSheet);

        excelWriter.finish();
        byte[] bytes = out.toByteArray();

        String fileName = "xx.xlsx";
        HttpHeaders headers = new HttpHeaders();


        headers.add("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        HttpStatus httpStatus = HttpStatus.OK;

        return new ResponseEntity(bytes, headers, httpStatus);
    }
}
